﻿//using System;
//using System.Collections.Generic;
//using System.Windows;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Controls;

//namespace Granite.Xaml
//{
//	/// <summary>
//	/// Helper functions for the visual tree
//	/// </summary>
//    public static partial class VisualTreeExtensions
//    {
//		/// <summary>
//		/// Returns the first ancestor of the indicated type.
//		/// </summary>
//		/// <typeparam name="T"></typeparam>
//		/// <param name="child"></param>
//		/// <returns></returns>
//        public static T FindFirstAncestorOfType<T>(this DependencyObject child)
//        {
//            var parent = VisualTreeHelper.GetParent(child);
//            if (parent != null)
//            {
//                if (parent is T)
//                    return (T)((object)parent);
//                else
//                    return FindFirstAncestorOfType<T>(parent);
//            }
//            else
//                return default(T);
//        }

//		/// <summary>
//		/// Finds the root of the visual tree
//		/// </summary>
//		/// <param name="control"></param>
//		/// <returns></returns>
//        public static DependencyObject FindRoot(this DependencyObject control)
//        {
//            if (control == null)
//                throw new ArgumentNullException("control", "control is null.");

//            var current = control;

//            while (true)
//            {
//                var next = VisualTreeHelper.GetParent(current);
//                if (next == null)
//                {
//                    return current;
//                }
//                current = next;
//            }
//        }

//        /// <summary>
//        /// Walks up the visual tree, generating a list of nodes it visits.
//        /// </summary>
//		/// <param name="start"></param>
//		/// <param name="includeUnnamedItems">If false, only controls with a non-null name are included.</param>
//        /// <returns></returns>
//		/// <remarks>This is for debugging purposes only.</remarks>
//        public static string GetVisualTreeAsString(this DependencyObject start, bool includeUnnamedItems)
//        {
//            var nameList = new List<String>();

//            var current = start;

//            while (current != null)
//            {
//                var namable = current as FrameworkElement;

//                if (namable != null && !String.IsNullOrEmpty(namable.Name))
//                {
//                    nameList.Add(namable.GetType().Name + "[" + namable.Name + "]");
//                }
//                else if (current == start || includeUnnamedItems)
//                {
//                    nameList.Add(current.GetType().Name);
//                }

//                current = VisualTreeHelper.GetParent(current);
//            }

//            return string.Join("-->", nameList.ToArray());
//        }

//    }

//#if WPF
//    public static partial class VisualTreeExtensions
//    {
//        /// <summary>
//        /// Returns true is the control or one of its children has the focus.
//        /// </summary>
//        /// <param name="control"></param>
//        /// <returns></returns>
//        public static bool ControlOrChildHasFocus(this DependencyObject control)
//        {
//            var current = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(control)) as DependencyObject;

//            while (current != null)
//            {
//                if (current == control) return true;
//                current = VisualTreeHelper.GetParent(current);
//            }
//            return false;
//        }

//		/// <summary>
//		/// Walks up the visual tree looking for an element of the indicated type with the indicated name. Starts with the focused element.
//		/// </summary>
//		/// <typeparam name="T"></typeparam>
//		/// <param name="start"></param>
//		/// <param name="name"></param>
//		/// <returns></returns>
//		public static bool VisualTreeContains<T>(this DependencyObject start, string name) where T : FrameworkElement
//        {
//			//TODO: This needs a better name
//            var current = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(start)) as DependencyObject;

//            while (current != null)
//            {
//                var element = current as T;

//                if (element != null && element.Name == name) return true;
//                current = VisualTreeHelper.GetParent(current);
//            }
//            return false;
//        }

//		/// <summary>
//		/// Walks up the visual tree looking for an element of the indicated type. Starts with the focused element.
//		/// </summary>
//		/// <typeparam name="T"></typeparam>
//		/// <param name="start"></param>
//		/// <returns></returns>
//		public static bool VisualTreeContains<T>(this DependencyObject start)
//        {
//			//TODO: This needs a better name
//            var current = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(start)) as DependencyObject;

//            while (current != null)
//            {
//                if (current is T) return true;
//                current = VisualTreeHelper.GetParent(current);
//            }
//            return false;
//        }

//    }
//#endif

//#if SILVERLIGHT
//	public static partial class VisualTreeExtensions
//    {

//		/// <summary>
//		/// Walks up the visual tree looking for an element of the indicated type with the indicated name. Starts with the focused element
//		/// </summary>
//		/// <typeparam name="T"></typeparam>
//		/// <param name="name"></param>
//		/// <returns></returns>
//        public static bool VisualTreeContains<T>(string name) where T : FrameworkElement
//        {
//			//TODO: This needs a better name
//			var current = FocusManager.GetFocusedElement() as DependencyObject;

//            while (current != null)
//            {
//                var element = current as T;

//                if (element != null && element.Name == name) return true;
//                current = VisualTreeHelper.GetParent(current);
//            }
//            return false;
//        }

//		/// <summary>
//		/// Walks up the visual tree looking for an element of the indicated type. Starts with the focused element
//		/// </summary>
//		/// <typeparam name="T"></typeparam>
//		/// <returns></returns>
//	    public static bool VisualTreeContains<T>()
//        {
//			//TODO: This needs a better name
//			var current = FocusManager.GetFocusedElement() as DependencyObject;

//            while (current != null)
//            {
//                if (current is T) return true;
//                current = VisualTreeHelper.GetParent(current);
//            }
//            return false;
//        }

//        /// <summary>
//        /// Returns true is the control or one of its children has the focus.
//        /// </summary>
//        /// <param name="control"></param>
//        /// <returns></returns>
//        public static bool ControlOrChildHasFocus(this FrameworkElement control)
//        {
//            var current = FocusManager.GetFocusedElement() as DependencyObject;

//            while (current != null)
//            {
//                if (current == control) return true;
//                current = VisualTreeHelper.GetParent(current);
//            }
//            return false;
//        }
//    }
//#endif

//#if SILVERLIGHT && !WINDOWS_PHONE
//    public static partial class VisualTreeExtensions
//    {
//	     /// <summary>
//        /// Scroll the targetControl into view by looking for a ScrollViewer. This is applied recursively in case we are inside nested ScrollViewers.
//        /// </summary>
//        /// <param name="targetControl"></param>
//        public static void BringIntoView(this FrameworkElement targetControl)
//        {
//            var current = VisualTreeHelper.GetParent(targetControl);

//            while (current != null)
//            {
//                var sv = current as ScrollViewer;
//                if (sv != null)
//                {
//                    BringIntoView(sv);
//                    sv.ScrollIntoView(targetControl);
//                    return;
//                }

//				current = VisualTreeHelper.GetParent(current);
//            }
//        }
//	}
//#endif

//}

